<?php
/**
 * Created by PhpStorm.
 * User: cth
 * Date: 2017/6/13
 * Time: 上午10:13
 */

namespace App\Repositories;

use App\Models\ShopClerk;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\Shop;
use App\Models\ShopInfo;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class ShopClerkRepository
{
    protected $shopClerk;

    public function __construct(ShopClerk $shopClerk)
    {
        $this->shopClerk = $shopClerk;
    }

    //所有员工信息
    public function getAllClerks(Request $request)
    {
        $rules = [
            'shop_id'=>'required|integer|exists:shops,id',
        ];
        $validator = Validator::make($request->all(), $rules);
        if ($validator->fails()) {
            return show(201,$validator->errors()->first());
        }
        $user_id = Auth::id();
        if (!$user_id){
            return redirect('/login');
        }
        // 根据shop_id和当前登陆人的user_id  从而判断这个人是是不是拥有这个商家，进而进行下一步操作
        if(!Shop::checkIsShop($request->shop_id,$user_id)){
            return show(203,'商家不属于该用户');
        }
        $res = $this->shopClerk::where('shop_id',$request->shop_id)->get();
        return show(200,'员工信息',$res);
    }


    public function addClerk(Request $request){
        $rules = [
            'name' => 'required',
            'password' => 'required',
            'mobile'=>'required|mobile',//|unique:shop_clerks,mobile',
            'role'=>'required|integer|in:0,1,2,3',                          //      (0财务，1服务员，2经理，3主管)
            'shop_id'=>'required|integer|exists:shops,id',
        ];
        $message = [
            'name.*' => '请填写名称',
            'password.*' => '请填写密码',
            'mobile.*' => '手机号有误',
            'role.*' =>  '角色有误',
            'shop_id.*' => '商家信息有误'
        ];
        $validator = Validator::make($request->all(), $rules,$message);
        if ($validator->fails()) {
            return show(201,$validator->errors()->first());
        }
        $user_id = Auth::id();
        if (!$user_id){
            return redirect('/login');
        }
        // 根据shop_id和当前登陆人的user_id  从而判断这个人是是不是拥有这个商家，进而进行下一步操作
        if(!Shop::checkIsShop($request->shop_id,$user_id)){
            return show(203,'商家不属于该用户');
        }
        $count = $this->shopClerk::where('shop_id',$request->shop_id)->where('mobile',$request->mobile)->count();
        if ($count >= 1){
            return show(202,'手机号重复，不允许添加');
        }
        $password = bcrypt($request->password);//md5(\request('password').''.'zlt');
        $employeeId = ''.$request->shop_id.''.$request->role;       // 员工编号
        for ($i = 1; $i <= 8; $i++) {
            $employeeId.= chr(rand(97, 122));
        }

        $data = array_merge(
            \request(['name','mobile','role','shop_id']),
            compact('password','employeeId')
        );
        $this->shopClerk::create($data);
        return show(200,'添加员工成功');
    }

    // 展示员工信息
    public function showOneClerk(Request $request){

        $rules = [
            'id'=>'required|integer|exists:shop_clerks,id',
            'shop_id'=>'required|integer|exists:shops,id',
        ];
        $message = [
            'id.*'=>'员工编号有误',
            'shop_id.*'=>'商家信息有误'
        ];
        $validator = Validator::make($request->all(), $rules,$message);
        if ($validator->fails()) {
            return show(201,$validator->errors()->first());
        }
        $user_id = Auth::id();
        if (!$user_id){
            return redirect('/login');
        }
        if(!Shop::checkIsShop($request->shop_id,$user_id)){
            return show(203,'商家不属于该用户');
        }
        $res = $this->shopClerk::where('shop_id',$request->shop_id)->find($request->id);
        return show(200,'员工信息',$res);
    }
    // 编辑员工信息
    public function editClerk(Request $request){

        $rules = [
            'id'=>'required|integer|exists:shop_clerks,id',
            'name' => 'string',
            'password' => 'string',
            'mobile'=>'mobile',
            'role'=>'required|integer|in:0,1,2,3',
            'employeeId' =>'string',
            'avatar'=>'string',
            'sex'=>'in:1,0'
        ];
        $validator = Validator::make($request->all(), $rules);
        if ($validator->fails()) {
            return show(201,$validator->errors()->first());
        }
        $clerk = $this->shopClerk::find($request->id);

        // Todo 手机号重复未处理
//        if ($count > 1){
//            return show(202,'手机号重复，不允许添加');
//        }
        try{
            $password = $request->password;
            if (isset($password)){
                $password=encrypt($password);
                $data = array_merge(
                    \request(['name','mobile','role','avatar','employeeId','sex']),
                    compact('password')
                );
            }else{
                $data = \request(['name','mobile','role','avatar','employeeId','sex']);
            }

            $clerk->update($data);
        }catch (\Exception $e){
            dd($e->getMessage());
            return show(202,'员工信息修改失败');
        }
        return show(200,'员工信息修改成功');
    }

    public function delClerk(Request $request){
        $rules = [
            'id'=>'required|integer|exists:shop_clerks,id',
            'shop_id'=>'required|integer|exists:shops,id',
        ];
        $validator = Validator::make($request->all(), $rules);
        if ($validator->fails()) {
            return show(201,$validator->errors()->first());
        }
        $user_id = Auth::id();
        if (!$user_id){
            return redirect('/login');
        }
        // 根据shop_id和当前登陆人的user_id  从而判断这个人是是不是拥有这个商家，进而进行下一步操作
        if(!Shop::checkIsShop($request->shop_id,$user_id)){
            return show(203,'商家不属于该用户');
        }

//        $res = DB::table('shop_clerks')->where('shop_id',$request->shop_id)->delete($request->id);
        $res = $this->shopClerk::where('shop_id',$request->shop_id)->where('id',$request->id)->delete();
        if(!$res){
            return show(200,'员工信息删除失败');
        }
        try{
            $this->shopClerk::where('shop_id',$request->shop_id)->where('id',$request->id)->update(['status'=>0]);
        }catch (\Exception $e){
            dd($e->getMessage());
        }
        return show(200,'员工信息删除成功');
    }
}
